﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RTUDataAcquisition.AppCode
{
    public static class INIConfig
    {

        /// 系统参数
        public static string IniPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "config.ini"; //配置文件路径
        public static string CompanyName = ""; //公司名称
        public static string ProgramName = ""; //程序名称
        public static string DialogBoxTitle = ""; //对话框标题
        public static string version = "1.0"; //程序版本

        /// <summary>
        /// SOCKET参数
        /// </summary>
        public static int ClientMaxCount = 10000;       //Tcp客户端的最大连接线程
        public static string IpAddress = "127.0.0.1";  //Tcp服务器监听端的IP地址
        public static string IpPort = "9000";         //Tcp服务器监听端的IP端口

        /// <summary>
        /// 数据库设置 database
        /// 写库
        /// </summary>
        //public static string StrConn = "";       //数据库的连接IP地址
        public static string DBSource = "";      //数据库地址
        public static string DBSourcePort = ""; //数据库端口
        public static string DBUsername = "";    //数据库的登录名称
        public static string DBPassword = "";    //数据库的登录密码
        public static string DBName = "";
        public static string TransDataDBName = "";
        /// <summary>
        /// 读库
        /// </summary>
        public static string DBSource_R = "";      //数据库地址
        public static string DBSourcePort_R = ""; //数据库端口
        public static string DBUsername_R = "";    //数据库的登录名称
        public static string DBPassword_R = "";    //数据库的登录密码
        public static string DBName_R = "";

        public static string DBNameNew = "";
        public static string StrConfigConn = "";       //Config数据库的连接IP地址
        public static string StrTransConn = "";       //Config数据库的连接IP地址
        public static string StrConfigConn_R = "";       //Config数据库的连接IP地址
        //public static string ConfigDBSource = "";      //Config数据库地址
        //public static string ConfigDBSourcePort = "";      //Config数据库端口
        //public static string ConfigDBUsername = "";    //数据库的登录名称
        //public static string ConfigDBPassword = "";    //数据库的登录密码
        public static string ConfigDBName = "";
        public static string ConfigDBName_R = "";
        public static string RealDataDBName = "";//实时数据表数据库
        public static string RealDataDBName_R = "";//实时数据表数据库
        public static string StrConnNoDB = "";       //数据库的连接IP地址
        public static string StrConnNoDB_R = "";       //数据库的连接IP地址
        public static string StrRealdataConn = "";       //实时数据库的连接IP地址
        public static string StrRealdataConn_R = "";       //实时数据库的连接IP地址
        public static string DBFilePath = "";//数据库文件路径
        public static string DBFilePath_R = "";//数据库文件路径
        /// <summary>
        /// 站点数据刷新间隔(秒)
        /// </summary>
        public static int TimerInterval = 5;

        /// <summary>
        /// 是否保存数据日志
        /// </summary>
        public static bool SaveDataLog = true;

        /// <summary>
        /// 图片保存路径
        /// </summary>
        public static string ImagePath = "";

        /// <summary>
        /// 消息队列数据分区
        /// </summary>
        public static string TopicData = "";

        /// <summary>
        /// 消息队列图片数据分区
        /// </summary>
        public static string TopicImage = "";

        /// <summary>
        /// 消息队列数据组
        /// </summary>
        public static string GroupData = "";

        /// <summary>
        /// 消息队列图片组
        /// </summary>
        public static string GroupImage = "";

        /// <summary>
        /// 消息队列数据分区数量
        /// </summary>
        public static int TopicPartitionData = 12;

        /// <summary>
        /// 消息队列图片分区数量
        /// </summary>
        public static int TopicPartitionImage = 12;
        public static string KafkaAddress = "";
        public static string HealthCheckIP = "";
        public static int HealthCheckPort = 0;
        public static int HealthCheckInterval = 0;
        public static string HealthCheckKey = "";
        /// <summary>
        /// 从配置文件中读取配置信息。
        /// </summary>
        /// <returns></returns>
        public static bool LoadSettings()
        {
            INIFile.Path = IniPath;
            string section = "database";
            DBSource = INIFile.ReadString(section, "DBSource", "");
            DBSourcePort = INIFile.ReadString(section, "DBSourcePort", "");
            DBUsername = INIFile.ReadString(section, "DBUsername", "");
            DBPassword = INIFile.ReadString(section, "DBPassword", "");
            DBName = INIFile.ReadString(section, "DBName", "");
            TransDataDBName = INIFile.ReadString(section, "TransDataDBName", "");
            DBSource_R = INIFile.ReadString(section, "DBSource_R", "");
            DBSourcePort_R = INIFile.ReadString(section, "DBSourcePort_R", "");
            DBUsername_R = INIFile.ReadString(section, "DBUsername_R", "");
            DBPassword_R = INIFile.ReadString(section, "DBPassword_R", "");
            DBName_R = INIFile.ReadString(section, "DBName_R", "");
            //DBNameNew = DBName + DateTime.Now.Year;
            GetDBConnectionString();
            //ConfigDBSource = INIFile.ReadString(section, "ConfigDBSource", "");
            //ConfigDBSourcePort = INIFile.ReadString(section, "ConfigDBSourcePort", "");
            //ConfigDBUsername = INIFile.ReadString(section, "ConfigDBUsername", "");
            //ConfigDBPassword = INIFile.ReadString(section, "ConfigDBPassword", "");
            ConfigDBName = INIFile.ReadString(section, "ConfigDBName", "");
            RealDataDBName = INIFile.ReadString(section, "RealDataDBName", "");
            RealDataDBName_R = INIFile.ReadString(section, "RealDataDBName_R", "");
            ConfigDBName_R = INIFile.ReadString(section, "ConfigDBName_R", "");
            DBFilePath = INIFile.ReadString(section, "DBFilePath", "");
            DBFilePath_R = INIFile.ReadString(section, "DBFilePath_R", "");
            GetConfigDBConnectionString();
            GetConnectionString();
            GetRealdataDBConnectionString();

            GetConnectionString_R();
            GetConfigRDBConnectionString();
            GetRealdataDBConnectionString_R();
            GetTransDBConnectionString();
            section = "programConfig";
            ProgramName = INIFile.ReadString(section, "ProgramName", "");
            DialogBoxTitle = INIFile.ReadString(section, "DialogBoxTitle", "");
            version = INIFile.ReadString(section, "version", "");

            section = "monitor";
            IpAddress = INIFile.ReadString(section, "IpAddress");
            IpPort = INIFile.ReadString(section, "IpPort");
            ClientMaxCount = (int)INIFile.ReadNumeric(section, "ClientMaxCount", 100);

            section = "runpara";
            TimerInterval = (int)INIFile.ReadNumeric(section, "TimerInterval", 5);
            SaveDataLog = INIFile.ReadBool(section, "SaveDataLog", true);
            ImagePath = INIFile.ReadString(section, "ImagePath", "C:\\ImageFile");
            TopicData = INIFile.ReadString(section, "TopicData", "Data_Test");
            TopicImage = INIFile.ReadString(section, "TopicImage", "Image_Test");
            GroupData = INIFile.ReadString(section, "GroupData", "Group_data_test");
            GroupImage = INIFile.ReadString(section, "GroupImage", "Group_image_test");
            TopicPartitionData = (int)INIFile.ReadNumeric(section, "TopicPartitionData", 12);
            TopicPartitionImage = (int)INIFile.ReadNumeric(section, "TopicPartitionImage", 12);
            KafkaAddress= INIFile.ReadString(section, "KafkaAddress", "");
            section = "healthcheck";
            HealthCheckIP = INIFile.ReadString(section, "HealthCheckIP", "");
            HealthCheckKey = INIFile.ReadString(section, "HealthCheckKey", "");
            HealthCheckPort = (int)INIFile.ReadNumeric(section, "HealthCheckPort", 0);
            HealthCheckInterval = (int)INIFile.ReadNumeric(section, "HealthCheckInterval", 5000);
            return true;
        }

        /// <summary>
        /// 根据设置生成数据库连接字符串
        /// </summary>
        /// <returns>数据库连接字符串</returns>
        public static string GetDBConnectionString()
        {
            string strTemp = DBSourcePort != string.Empty ? DBSource + "," + DBSourcePort : DBSource;
            return string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                 , strTemp, DBName + DateTime.Now.Year, DBUsername, DBPassword);
        }
        public static string GetRDBConnectionString()
        {
            string strTemp = DBSourcePort_R != string.Empty ? DBSource_R + "," + DBSourcePort_R : DBSource_R;
            return string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                 , strTemp, DBName_R + DateTime.Now.Year, DBUsername_R, DBPassword_R);
        }
        /// <summary>
        /// 根据设置生成数据库连接字符串
        /// </summary>
        /// <returns>数据库连接字符串</returns>
        public static string GetConfigDBConnectionString()
        {
            //string strTemp = ConfigDBSourcePort != string.Empty ? ConfigDBSource + "," + ConfigDBSourcePort : ConfigDBSource;
            //StrConfigConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
            //    , strTemp, ConfigDBName, ConfigDBUsername, ConfigDBPassword);
            //return StrConn;
            string strTemp = DBSourcePort != string.Empty ? DBSource + "," + DBSourcePort : DBSource;
            StrConfigConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                , strTemp, ConfigDBName, DBUsername, DBPassword);
            return StrConfigConn;
        }
        public static string GetTransDBConnectionString()
        {
            //string strTemp = ConfigDBSourcePort != string.Empty ? ConfigDBSource + "," + ConfigDBSourcePort : ConfigDBSource;
            //StrConfigConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
            //    , strTemp, ConfigDBName, ConfigDBUsername, ConfigDBPassword);
            //return StrConn;
            string strTemp = DBSourcePort != string.Empty ? DBSource + "," + DBSourcePort : DBSource;
            StrTransConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                , strTemp, TransDataDBName, DBUsername, DBPassword);
            return StrTransConn;
        }
        
        public static string GetConfigRDBConnectionString()
        {
            //string strTemp = ConfigDBSourcePort != string.Empty ? ConfigDBSource + "," + ConfigDBSourcePort : ConfigDBSource;
            //StrConfigConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
            //    , strTemp, ConfigDBName, ConfigDBUsername, ConfigDBPassword);
            //return StrConn;
            string strTemp = DBSourcePort_R != string.Empty ? DBSource_R + "," + DBSourcePort_R : DBSource_R;
            StrConfigConn_R = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                , strTemp, ConfigDBName_R, DBUsername_R, DBPassword_R);
            return StrConfigConn_R;
        }
        /// <summary>
        /// 根据设置生成数据库连接字符串
        /// </summary>
        /// <returns>数据库连接字符串</returns>
        public static string GetRealdataDBConnectionString()
        {
            string strTemp = DBSourcePort != string.Empty ? DBSource + "," + DBSourcePort : DBSource;
            StrRealdataConn = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                , strTemp, RealDataDBName, DBUsername, DBPassword);
            return StrRealdataConn;
        }
        public static string GetRealdataDBConnectionString_R()
        {
            string strTemp = DBSourcePort_R != string.Empty ? DBSource_R + "," + DBSourcePort_R : DBSource_R;
            StrRealdataConn_R = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};"
                , strTemp, RealDataDBName_R, DBUsername_R, DBPassword_R);
            return StrRealdataConn_R;
        }
        /// <summary>
        /// 根据设置生成数据库连接字符串
        /// </summary>
        /// <returns>数据库连接字符串</returns>
        public static string GetConnectionString()
        {
            string strTemp = DBSourcePort != string.Empty ? DBSource + "," + DBSourcePort : DBSource;
            StrConnNoDB = string.Format("Data Source={0};User Id={1};Password={2};"
                , strTemp, DBUsername, DBPassword);
            return StrConnNoDB;
        }
        public static string GetConnectionString_R()
        {
            string strTemp = DBSourcePort_R != string.Empty ? DBSource_R + "," + DBSourcePort_R : DBSource_R;
            StrConnNoDB_R = string.Format("Data Source={0};User Id={1};Password={2};"
                , strTemp, DBUsername_R, DBPassword_R);
            return StrConnNoDB_R;
        }
    }
}
